package com.xiaoyu.binaryTree.basic;

/**
 * @program: DS_and_A
 * @description: TODO
 * @author: YuWenYi
 * @create: 2021-05-13 14:55
 **/
public class ArrBinaryTreeDemo {

    public static void main(String[] args) {
        int[] tree = new int[]{1,2,3,4,5,6,7};
        ArrBinaryTree arrBinaryTree = new ArrBinaryTree(tree);
        arrBinaryTree.preOrder(0);
    }
}

class ArrBinaryTree{
    private int[] tree;

    public ArrBinaryTree(int[] tree) {
        this.tree = tree;
    }

    //前序遍历  --> 中序遍历和后序遍历是类似的代码
    public void preOrder(int index){
        if (tree == null || tree.length == 0){
            System.out.println("数组为空无法遍历");
            return;
        }
        System.out.print(tree[index]+" ");
        if (index*2 + 1 < tree.length){
            preOrder(index*2+1);
        }
        if (index*2+2 < tree.length){
            preOrder(index*2+2);
        }
    }

}